﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Model.DocumentDB
{
    /// <summary>Represents a folder containing a collection of items.</summary>
    public abstract class FolderViewModel
    {
        /// <summary>Constructor.</summary>
        /// <param name="displayName">See <see cref="FolderDisplayName"/>.</param>
        /// <param name="isAvailable">See <see cref="IsAvailable"/>.</param>
        public FolderViewModel(string displayName, bool isAvailable)
        {
            if (string.IsNullOrWhiteSpace(displayName))
            {
                throw new ArgumentNullException("displayName");
            }

            FolderDisplayName = displayName;
            IsAvailable = isAvailable;
        }

        /// <summary>Name of the folder.</summary>
        public string FolderDisplayName { get; private set; }

        /// <summary>Indicates if the folder is available.</summary>
        /// <remarks>Typically this is related to the associated feature being available or not in the current release.</remarks>
        public bool IsAvailable { get; private set; }

        /// <summary>Ensures the folder content is loaded.</summary>
        /// <returns>Asynchronous completion.</returns>
        public abstract Task EnsureLoadedAsync();
    }

    /// <summary>Represents a folder containing a collection of strong type items.</summary>
    public class FolderViewModel<T, PARENT> : FolderViewModel, IRefreshable
    {
        /// <summary>Constructor.</summary>
        /// <param name="parent">Parent of the folder.</param>
        /// <param name="displayName">See <see cref="FolderDisplayName"/>.</param>
        /// <param name="isAvailable">See <see cref="IsAvailable"/>.</param>
        /// <param name="folderContent">Collection content.</param>
        public FolderViewModel(PARENT parent, string displayName, bool isAvailable, LoadableCollection<T> folderContent)
            : base(displayName, isAvailable)
        {
            if (folderContent == null)
            {
                throw new ArgumentNullException("folderContent");
            }
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            Parent = parent;
            FolderContent = folderContent;
        }

        /// <summary>Content of the folder.</summary>
        public LoadableCollection<T> FolderContent { get; private set; }

        /// <summary>Folder's parent.</summary>
        public PARENT Parent { get; private set; }

        /// <summary>Ensure <see cref="FolderContent"/> is loaded.</summary>
        /// <returns>Asynchronous completion.</returns>
        public override Task EnsureLoadedAsync()
        {
            return FolderContent.EnsureLoadedAsync();
        }

        Task IRefreshable.RefreshAsync()
        {
            if (FolderContent.IsLoading)
            {   //  Already loading
                return Task.FromResult(42);
            }
            else if (FolderContent.IsLoaded || FolderContent.HasError)
            {
                return FolderContent.RefreshAsync();
            }
            else
            {
                return FolderContent.EnsureLoadedAsync();
            }
        }
    }
}